<?php

class Schema
{
  var $cs;

  static function load()
  {
    $schema = new Schema();
    $api = $GLOBALS["PEA_OUT_PATH"]."/".$GLOBALS["DB_DATABASE"]."_api.php";
    if (!file_exists($api))
      {
	$schema->generate();
      }

    if ($GLOBALS["PEA_AUTO_GENERATE"] && !$schema->checksum())
      {
	$schema->generate();
      }

    Log::dbg("db schema " . $GLOBALS["PEA_OUT_PATH"]."/".$GLOBALS["DB_DATABASE"]."_api.php");
    include_once($api);
  }

  function checksum()
  {
    $cs =  file_get_contents($GLOBALS["PEA_OUT_PATH"]."/".$GLOBALS["DB_DATABASE"]."_api_cs.php");
    $pdox = PDOx::get();
    $st = $pdox->prepare("select MD5(SUM(CREATE_TIME)) as CS from information_schema.tables where TABLE_SCHEMA=:database;");
    $st->execute(array("database" => $GLOBALS["DB_DATABASE"]));
    $result = $st->fetchAll();
    $this->cs = $result[0]["CS"]; 
    if ($cs == $this->cs)
      {
	return true;
      }
  
    return false;
  }


  function generate()
  {
    if (!$GLOBALS["PEA_AUTO_GENERATE"])
      {
	return true;
      }

    $cs = @file_get_contents($GLOBALS["PEA_OUT_PATH"]."/".$GLOBALS["DB_DATABASE"]."_api_cs.php");

    Log::dbg("db schema generate files.");

    $prop = array("DB_DATABASE" => "information_schema");

    $schema = array();
    $pdox = new PDOx($prop);
    $qb = new QueryBuilder();
    $mgr = new DB($pdox, $qb);
    $t = $mgr->tables;
    $t->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];

    while ($t->next())
      {
	$schema[$t->TABLE_NAME] = array();
	$schema[$t->TABLE_NAME]["col"] = array();
	$schema[$t->TABLE_NAME]["rel"] = array();
	$schema[$t->TABLE_NAME]["rev"] = array();
      }

    foreach ($schema as $t => $a)
      {

	$col = $mgr->columns;
	$col->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];
	$col->TABLE_NAME->eq = $t;
	while ($col->next())
	  {
	    $schema[$t]["col"][$col->COLUMN_NAME] = $col->DATA_TYPE;
	  }

	$pri = $mgr->columns;
	$pri->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];
	$pri->TABLE_NAME->eq = $t;
	$pri->COLUMN_KEY->eq = "PRI";
	while ($pri->next())
	  {
	    $schema[$t]["pri"] = $pri->COLUMN_NAME;
	  }


	$rel = $mgr->key_column_usage;
	$rel->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];
	$rel->TABLE_NAME->eq = $t;
	$rel->REFERENCED_TABLE_NAME->not->is = null;
	while ($rel->next())
	  {
	    $schema[$t]["rel"][$rel->REFERENCED_TABLE_NAME] = $rel->COLUMN_NAME;
	  }

	$rev = $mgr->key_column_usage;
	$rev->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];
	$rev->REFERENCED_TABLE_NAME->eq = $t;
	$rev->TABLE_NAME->not->is = null;
	while ($rev->next())
	  {
	    $schema[$t]["rev"][$rev->TABLE_NAME] = $rev->COLUMN_NAME;
	  }

	$auto = $mgr->columns;
	$auto->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];
	$auto->TABLE_NAME->eq = $t;
	$auto->COLUMN_KEY->eq = "PRI";
	$auto->EXTRA->eq = "auto_increment";
	while ($auto->next())
	  {
	    $schema[$t]["auto"] = $auto->COLUMN_NAME;
	  }

	$uuid = $mgr->columns;
	$uuid->TABLE_SCHEMA->eq = $GLOBALS["DB_DATABASE"];
	$uuid->TABLE_NAME->eq = $t;
	$uuid->COLUMN_KEY->eq = "PRI";
	$uuid->COLUMN_TYPE->eq = "char(36)";
	while ($uuid->next())
	  {
	    $schema[$t]["uuid"] = $uuid->COLUMN_NAME;
	  }

      }

    $tlp = file_get_contents($GLOBALS["PEA_PATH"]."/schema.tpl.php");

    ob_start();
    eval("echo '<?php\n'; ?>\n".$tlp. "\n<?php echo '?>\n';");
    $code = ob_get_contents();
    ob_end_clean();

    Log::dbg("WRITE API FILES " . $GLOBALS["PEA_OUT_PATH"].$GLOBALS["DB_DATABASE"]."_api.php");
    file_put_contents($GLOBALS["PEA_OUT_PATH"].$GLOBALS["DB_DATABASE"]."_api.php", $code);
    file_put_contents($GLOBALS["PEA_OUT_PATH"].$GLOBALS["DB_DATABASE"]."_api_cs.php", $this->cs);

    return $code;
  }

}

function tpl_string($val)
{
  if (isset($val))
    return "\"".$val."\"";
  return "null";
}
function tpl_array($array)
{
  if (! is_array($array))
    {
      return "null";
    }
  $str = "array(";
  $sep = "";
  foreach ($array as $k => $v)
    {
      $str .= "$sep'$k'=>'$v'";
      $sep = ",";
    }
  $str .= ")";
  return $str;
}

?>